package com.cctc.mychat.client.handler;

import com.cctc.mychat.common.protocol.message.impl.response.LoginResponseMessage;
import com.cctc.mychat.common.session.Session;
import com.cctc.mychat.common.session.SessionManager;
import com.cctc.mychat.common.util.JsonUtils;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class LoginResponseMessageHandler extends SimpleChannelInboundHandler<LoginResponseMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, LoginResponseMessage response) throws Exception {
        System.out.println("[LoginResponseMessageHandler] 登录消息：" + JsonUtils.toJson(response));

        if (response.isSuccess()) {
            Session session = Session.builder().userName(response.getUserName()).build();
            SessionManager.bindSession(session, ctx.channel());
            System.out.println("登录成功！！！");
        } else {
            System.err.println("登录失败，原因：" + response.getReason());
        }

    }
}
